package com.iknow.view;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask.Status;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.IKnowApiV4;
import com.iknow.User;
import com.iknow.activity.TagQingboActivity;
import com.iknow.app.IKnowSystemConfig;
import com.iknow.data.SubscribeTag;
import com.iknow.db.IKnowDataBase;
import com.iknow.http.HttpException;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.SubscribeTagAdapter;
import com.iknow.util.Loger;
import com.iknow.util.StringUtil;
import com.iknow.view.widget.PullToRefreshListView;
import com.iknow.view.widget.PullToRefreshListView.OnRefreshListener;
import java.util.List;

public class SubscribeTagView extends LinearLayout
{
  private PullToRefreshListView.OnRefreshListener ListViewRefreshListener;
  private final String TAG = "SubscribeTagView";
  private AdapterView.OnItemClickListener listItemClickListener;
  private LayoutInflater mInflater;
  private LinearLayout mLayout;
  private PullToRefreshListView mListView;
  private TaskAdapter mListener;
  private SubscribeTagAdapter mTagAdapter;
  private List<SubscribeTag> mTagList;
  private FlagTask mTask;
  private User mUser;

  public SubscribeTagView(Context paramContext)
  {
    super(paramContext);
    this.ListViewRefreshListener = new PullToRefreshListView.OnRefreshListener(this)
    {
      public void onRefresh()
      {
        this.this$0.getSubscribeTag();
      }
    };
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        SubscribeTag localSubscribeTag = SubscribeTagView.access$0(this.this$0).getItem(paramInt - 1);
        Intent localIntent = new Intent(this.this$0.getContext(), TagQingboActivity.class);
        localIntent.putExtra("tag", localSubscribeTag);
        this.this$0.getContext().startActivity(localIntent);
      }
    };
    this.mListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "FlagTask";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramTaskResult == TaskResult.OK)
        {
          SubscribeTagView.access$0(this.this$0).clearTag();
          if (SubscribeTagView.access$1(this.this$0) != null)
            SubscribeTagView.access$0(this.this$0).setMySubscribeTagList(IKnow.mIKnowDataBase.getSubscribeTags(SubscribeTagView.access$1(this.this$0).getUID()));
          SubscribeTagView.access$0(this.this$0).setTagList(SubscribeTagView.access$2(this.this$0));
        }
        while (true)
        {
          SubscribeTagView.access$0(this.this$0).notifyDataSetChanged();
          SubscribeTagView.access$4(this.this$0).onRefreshComplete();
          return;
          Toast.makeText(this.this$0.getContext(), SubscribeTagView.access$3(this.this$0).getErrorMessage(), 0).show();
        }
      }

      public void onPreExecute()
      {
      }
    };
    initView();
  }

  public SubscribeTagView(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    this.ListViewRefreshListener = new PullToRefreshListView.OnRefreshListener(this)
    {
      public void onRefresh()
      {
        this.this$0.getSubscribeTag();
      }
    };
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        SubscribeTag localSubscribeTag = SubscribeTagView.access$0(this.this$0).getItem(paramInt - 1);
        Intent localIntent = new Intent(this.this$0.getContext(), TagQingboActivity.class);
        localIntent.putExtra("tag", localSubscribeTag);
        this.this$0.getContext().startActivity(localIntent);
      }
    };
    this.mListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "FlagTask";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramTaskResult == TaskResult.OK)
        {
          SubscribeTagView.access$0(this.this$0).clearTag();
          if (SubscribeTagView.access$1(this.this$0) != null)
            SubscribeTagView.access$0(this.this$0).setMySubscribeTagList(IKnow.mIKnowDataBase.getSubscribeTags(SubscribeTagView.access$1(this.this$0).getUID()));
          SubscribeTagView.access$0(this.this$0).setTagList(SubscribeTagView.access$2(this.this$0));
        }
        while (true)
        {
          SubscribeTagView.access$0(this.this$0).notifyDataSetChanged();
          SubscribeTagView.access$4(this.this$0).onRefreshComplete();
          return;
          Toast.makeText(this.this$0.getContext(), SubscribeTagView.access$3(this.this$0).getErrorMessage(), 0).show();
        }
      }

      public void onPreExecute()
      {
      }
    };
    initView();
  }

  private void initView()
  {
    this.mInflater = ((LayoutInflater)getContext().getSystemService("layout_inflater"));
    this.mLayout = ((LinearLayout)this.mInflater.inflate(2130903129, null));
    this.mTagAdapter = new SubscribeTagAdapter(this.mInflater);
    this.mListView = ((PullToRefreshListView)this.mLayout.findViewById(2131296535));
    this.mListView.setOnRefreshListener(this.ListViewRefreshListener);
    this.mListView.setAdapter(this.mTagAdapter);
    this.mListView.setOnItemClickListener(this.listItemClickListener);
    String str = IKnow.mSystemConfig.getString("SubscribeTagView");
    if (!(StringUtil.isEmpty(str)))
      this.mListView.setLastUpdated("更新于" + str);
    addView(this.mLayout);
  }

  public void getSubscribeTag()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mListView.prepareForRefresh();
      this.mTask = new FlagTask(this, null);
      this.mTask.setListener(this.mListener);
      this.mTask.execute(new TaskParams[0]);
    }
  }

  private class FlagTask extends GenericTask
  {
    private String mErrorMessage;

    protected TaskResult _doInBackground()
    {
      TaskResult localTaskResult;
      try
      {
        SubscribeTagView.access$5(this.this$0, IKnow.mApi.getSubscribeTags());
        localTaskResult = TaskResult.OK;
        return localTaskResult;
      }
      catch (HttpException localHttpException)
      {
        this.mErrorMessage = localHttpException.getMessage();
        Loger.e("SubscribeTagView", this.mErrorMessage);
        localTaskResult = TaskResult.FAILED;
      }
    }

    public String getErrorMessage()
    {
      return this.mErrorMessage;
    }
  }
}